#!/bin/bash
 
# -------------------------------------------------------------------------------
# Filename:    CentOS6_7_intall_docker.sh
# Revision:    1.0
# Date:        2017/11/28
# Author:      star
# Email:       liuxing007xing@163.com
# Description: 安装docker
# Notes:       需要切换到root运行,版本针对64位系统，操作系统为CentOS6或者centos7
#            
# -------------------------------------------------------------------------------
# Copyright:   2017 (c) star
# -------------------------------------------------------------------------------


#检查是否是root用户######################################################################
if [ $(id -u) != "0" ]; then  
    echo "Error: You must be root to run this script, please use root to run"  
    exit 1  
fi
#系统版本检测############################################################################
platform=`uname -i`
if [ $platform != "x86_64" ];then 
echo "this script is only for 64bit Operating System !"
exit 1
fi
echo "the platform is ok"
version6=`more /etc/redhat-release |awk '{print substr($3,1,1)}'`
if [ $version6 = 6 ];then
echo "System is CentOS 6 !"
SYSTEM="CentOS6"
fi
version7=`more /etc/redhat-release |awk '{print substr($4,1,1)}'`
if [ $version7 = 7 ];then
echo "System is CentOS 7 !"
SYSTEM="CentOS7"
fi

####centos6
if [ $SYSTEM = 'CentOS6' ]; then
	######安装docker
	yum -y update
	yum install http://mirrors.yun-idc.com/epel/6/i386/epel-release-6-8.noarch.rpm 
	yum install -y docker-io
	chkconfig docker on 
	service docker start	
	
	######修改docker默认存储位置
	mkdir -p /data/docker/images
	service docker stop
	cd /var/lib
	cp -rf docker docker.bak
	mv /var/lib/docker /data/docker/images
	ln -s /data/docker/images/docker /var/lib/docker
	service docker restart
	chkconfig docker on
	
######配置国内镜像源	
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": [
    "https://0b27f0a81a00f3560fbdc00ddd2f99e0.mirror.swr.myhuaweicloud.com",
    "https://xbgtbrbn.mirror.aliyuncs.com",
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
  ]

    "graph": "/data/docker",
    "bip": "11.0.0.1/16",
    #设置仓库
    #"insecure-registries": ["192.168.1.20:5000"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m",
        "max-file": "5"
    },
    "max-concurrent-downloads": 100,
    "max-concurrent-uploads": 100,
    "storage-driver": "overlay2",
    "live-restore": true
}
EOF

fi

####centos7
if [ $SYSTEM = 'CentOS7' ]; then
	######安装docker
	#yum -y update
	yum install -y yum-utils
	#curl -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo
	#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

	#设置国内的镜像仓库：
	yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
	yum install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

	#curl -sSL https://get.docker.com/ | sh

	systemctl start docker
	systemctl enable docker.service

	######修改docker默认存储位置
	mkdir -p /data/docker/images
	systemctl stop docker.service

	#cd /var/lib
	#cp -rf docker docker.bak
	#mv /var/lib/docker /data/docker/images
	#ln -s /data/docker/images/docker /var/lib/docker

	##### 安装docker-compose
	#curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
	#chmod +x /usr/local/bin/docker-compose
	#ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose

######配置国内镜像源
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
    "data-root": "/data/docker"
	"registry-mirrors": [
    "https://0b27f0a81a00f3560fbdc00ddd2f99e0.mirror.swr.myhuaweicloud.com",
    "https://xbgtbrbn.mirror.aliyuncs.com",
    "https://registry.docker-cn.com",
    "http://hub-mirror.c.163.com",
    "https://docker.mirrors.ustc.edu.cn"
	]
	#指定 Docker 容器镜像的存储路径,配置data-root,可以不用配置此项
    #"graph": "/data/docker",
    "bip": "11.0.0.1/16",
    #设置仓库
    #"insecure-registries": ["192.168.1.20:5000"],
    "exec-opts": ["native.cgroupdriver=systemd"],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m",
        "max-file": "5"
    },
    "max-concurrent-downloads": 100,
    "max-concurrent-uploads": 100,
    "storage-driver": "overlay2",
    "live-restore": true
}
EOF
	systemctl daemon-reload
	systemctl restart docker.service
	systemctl enable docker.service
fi


#卸载
# yum install -y docker #安装版本太低
# yum remove docker*
# rm -rf /var/lib/docker
# rm -rf /etc/docker